package br.com.mynage.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;

import br.com.mynage.dao.IProjetoDAO;
import br.com.mynage.dominio.Projeto;
import br.com.mynage.dominio.Usuario;
import br.com.mynage.exception.DaoException;

public class ProjetoHibernateDAO extends GenericDaoImpl<Projeto> implements IProjetoDAO {

	@Override
	public List<Projeto> buscarProjetos() throws DaoException {
		return this.findAll();
	}

	@Override
	public void cadastrarProjeto(Projeto projeto) throws DaoException {
		this.insert(projeto);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> buscarIdProjetosPorUsuario(Usuario usuario)
			throws DaoException {
		EntityManager manager = getEntityManager();
		try{
			Session session = (Session) manager.getDelegate();
			
			String sql = "SELECT projeto_usuario.id_projeto FROM projeto_usuario " +
					"WHERE projeto_usuario.id_usuario = " + usuario.getId() ;

			return session.createSQLQuery(sql).list();
		}catch (Exception e) {
			throw new DaoException();
		}finally{
			if(manager != null) manager.clear();
		}
	}

	@Override
	public Class<Projeto> getClassType() {
		return Projeto.class;
	}


}
